<?php

if( !defined( 'MEDIAWIKI' ) )
die(-1);

require_once('DatabaseFunctions.php');

#install extension hook
//$wgExtensionFunctions[] = "wfHotFiles";
$wgAjaxExportList[] = 'wfAjaxHotFiles';
$wgHooks['BeforePageDisplay'][]= 'wfShowCategoryHotFiles';

function wfAjaxHotFiles($page, $category){

	if ($category>0) $genreKey = "filter=genreKey,$category";
	
	$dbr = wfGetDB( DB_SLAVE );
    $sql = "select s.Count as w, t.Artist, t.Title, t.firstseendate, s.int2 as Pop from searches s inner join ss_tags t on s.idtags=t.idtags where s.query ='$genreKey;groupby=attr:artistkey;limit=10;offset=".(($page-1)*10).";select=idtags as idtags, sum(10/(firstSeen+1)*weightTags*TotalCount) as int1, sum(1+TotalCount*weightTags) as int2; groupsort=int1 desc;sort=expr:1000/(CountFiles*weightTags+1);floatrange=weightTags,0.6,1;filter=type,1'";
    $res = $dbr->query( $sql );
	
	$i = ($page-1)*10;
    while ($row = $dbr->fetchObject( $res )) {
      $i++;
      $artist = wfUrlencode($row->Artist);
      $artistHtml = htmlspecialchars($row->Artist, ENT_QUOTES);
      $title = wfUrlencode($row->Title);
      $class = ($i%2)==1?"":" class='alt'";
      $value = "";
      for ($p=0x400; $p<$row->Pop && $i<0x8000; $p<<=1)
		$value .= "<img src='/skins/soundshare/icon_user.gif'>";
      $out .= "<tr$class><td width='20'>".sprintf("%02d", $i)."]</td><td><a title='$row->Artist' href='/download/{$artist}/{$title}'>$row->Artist, $row->Title</a></td><td>$value</td></tr>";
    }
    $dbr->freeResult( $res );
	return $out;
}


function wfShowCategoryHotFiles() {
  global $wgTitle, $wgCategory, $wgRequest, $wgDBprefix, $wgOut, $wgUser, $IP;

  
  if ($wgTitle->mNamespace!=NS_CATEGORY) return true;
  
  if ($wgCategory!=-1) {
	$start_time = microtime(true);
    if ($wgCategory>0) $genreKey = "filter=genreKey,$wgCategory";

    $wgOut->addScript("<script type=\"text/javascript\" src=\"/skins/soundshare/hotFiles.js\"></script>\n");

    $dbr = wfGetDB( DB_SLAVE );
    $sql = "select s.Count as w, t.Artist, t.Title, t.firstseendate, s.int2 as Pop from searches s inner join ss_tags t on s.idtags=t.idtags where s.query ='$genreKey;groupby=attr:artistkey;limit=10;select=idtags as idtags,sum(10/(firstSeen+1)*countTagsOk) as int1, sum(countTagsOk+0.01) as int2; groupsort=int1 desc;sort=expr:countTagsOk;'";
    $res = $dbr->query( $sql );
	
    $out .= '<div class="home_module" id="whatshot" >';
    $out .= "<h2>what's hot</h2>";
	$out.='<input type="hidden" id="page_hots" value="2">';
	$out .= "<table id='thotfiles' width='100%' cellspacing='0' class='searchresults'><tr class='filesheader'><td width='20px'>&nbsp;</td><td>Artist & album</td><td width='80px'>Popularity</td></tr>";
    $i = 0;
    while ($row = $dbr->fetchObject( $res )) {
      $i++;
      $html = htmlspecialchars($row->Artist." - ".$row->Title, ENT_QUOTES);
      $class = ($i%2)==1?"":" class='alt'";
      $value = "";
	$steps = intval(log($row->Pop))+1; $s = 1;
	if ($steps==1 || $steps>7) $offset = 0; else $offset = $steps%2*4;
	for ($k = 0; $k<$steps; $k++)
	{
		$j = $k*2+1;
		$left = 35 + ($j%16)*2*$s-intval($j/16)%2*4 - $offset;
		$top = intval($k/8)*2 -8 - intval($steps/8);
		$s = -$s;	
	        $value .= "<img style='position:absolute; top:{$top}px; left:{$left}px;' src='/skins/soundshare/icon_user.gif'>";
	}	
	
	$linkTitle = SoundshareTitle::newFromText($row->Artist."/".$row->Title, NS_SONG);
	$linkClass = $linkTitle->isKnown()?"":" class='new'";
        $out .= "<tr$class><td width='20'>".sprintf("%02d", $i)."]</td><td><a href='".htmlspecialchars($linkTitle->getLocalUrl(), ENT_QUOTES)."' $linkClass title='$html'>$html</a></td><td><div style='position:relative;'>$value</div></td></tr>";
    }
    $dbr->freeResult( $res );
    $out .= '</table><span style="font-size: 9px; position: absolute; color: transparent;">'.(microtime(true) - $start_time).'</span>';
    $wgOut->addHtml($out);
	createMorePageBar($wgCategory); 
	$output="<script>document.getElementById('page_hots').value=2;</script></div>";
	$wgOut->addHTML($output);
  }

return true;
}
function createMorePageBar($category) {
	global $wgOut,$wgRequest,$wgScript;

	$qry="javascript:moreHotFiles('".$category."')";
			
	$next_page = '<div style="text-align:center;margin-top:20px">';
	$next_page.= '<div class="button-grey"><div class="button-grey-start"></div>';
	$next_page .= '<a href="'.$qry;
	$next_page .= '">[+] 10 ' . wfMsg('more') .'</a>';
	$next_page .= '<div class="button-grey-end"></div></div>';
	$next_page .='<div><img id="loader_hots" style="display:none" src="/skins/soundshare/loader_tops.gif"></div>';
	$next_page .= '<div style="padding-bottom:35px"></div></div> ';
	
	$wgOut->addHTML($next_page);
		
}
